// TOWN SCRIPT
//    Town 7: Lost Mine

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,choice;
short lead_char;
short c;

body;

beginstate INIT_STATE;
// This state called whenever this town is entered

add_range_to_group(7,17,1);

 c = 0;

 i = 0;
 while (i < 5) {
  if (char_ok(i)) {
   lead_char = i;
   i = 5;
  }
  else
   i = i + 1;
 }

i = 7;
while (i <= 11) {
 if (get_sdf(5,(i - 6)) == 0)
  spawn_creature(i);
 else
  erase_char(i);
 i = i + 1;
}

i = 12;
while (i <= 16) {
 erase_char(i);
 i = i + 1;
}
	
if (get_sdf(100,0) > 0)
 set_terrain(18,12,138);

if (get_sdf(5,7) == 2) {
 set_terrain(14,31,0);
 put_object_on_space(14,31,6);
 set_terrain(13,56,0);
 put_object_on_space(13,56,5);
}

if (get_sdf(5,8) > 0)
 set_terrain(5,39,388);

if (get_sdf(100,0) > 0) {
 i = 6;
 set_incidental_sound(1);
 while (i < 120) {
  erase_char(i);
  i = i + 1;
 }
}

 set_crime_tolerance(1);

break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;

c = c + 1;

if ((get_terrain(5,39) == 388) && (get_sdf(5,8) == 0)) {
 set_flag(5,8,1);
}

if ((get_sdf(5,6) == 0) && (c >= 40) && (get_ran(1,1,10) <= 1)) {
 set_char_status(random_group_member(0),8,2,1,0);
 play_sound(46);
 c = 0; 
}

if ((get_terrain(18,12) != 432) && (get_sdf(100,0) == 0))
 set_state_continue(11);
	
break;

beginstate 10;

reset_dialog();
add_dialog_str(0,"This passage leads back to the city.",0);
add_dialog_choice(0,"Return to the Lost City.");
add_dialog_choice(1,"Stay here.");
choice = run_dialog(1);
if (choice == 1)
 move_to_new_town(6,8,15);
if (choice == 2)
	block_entry(1);

break;


beginstate 11;

set_flag(100,0,1);
clear_town();

i = 6;
while (i < 120) {
 erase_char(i);
 i = i + 1;
}

play_sound(40);
play_sound(40);
play_sound(40);
set_incidental_sound(1);

message_dialog("The strange magical device has been shattered by the energy beam. The sounds of spirits fading into the void echo throughout the cavern, then all is silent. The stalking Perfect Spirit gone too. Thank the gods!","The ancient device must have been holding them here, and now they are free. Hopefully, the Perfect Spirit will leave this world forever.");
toggle_quest(4,0);
award_party_xp(500,5);

break;


beginstate 12;

if (get_terrain(5,39) == 388) {
 if ((get_sdf(5,6) == 1) && (get_sdf(100,0) == 0)) {
  set_state_continue(21);
  end();
 }
 message_dialog("A teleporter flickers insubstatially here. It is not powerful enough to take you anywhere.","The device seems to draw power from the source behind it. Perhaps if you could recharge it, the teleporter would become useable as well.");
}
else {
 reset_dialog();
 add_dialog_str(0,"A teleporter glows brightly here. You have no idea where it will take you, but it may be your only escape from this place.",0);
 add_dialog_choice(0,"Enter the portal.");
 add_dialog_choice(1,"Stay here.");
 choice = run_dialog(1);
 if (choice == 1) {
  if (get_sdf(100,0) == 0)
   set_state_continue(21);
  else {
   play_sound(10);
   set_flag(1,18,1);
   move_to_new_town(14,32,9);
  }
 }
 if (choice == 2)
	 block_entry(1);
}

break;


beginstate 13;

if (get_sdf(1,20) == 0) {
 set_flag(1,20,1);
 message_dialog("You approach the control panel. It appears to have plenty of power, but something has smashed in the face rendering it inoperative. Whatever this device did, you will probably never know.","");
}

break;


beginstate 14;

if (get_sdf(1,21) == 0) {
 set_flag(1,21,1);
 message_dialog("This was once a large alien structure of some sort. Much of it has caved in, damaging the bizarre equipment. Most notable is a beam fired at a shattered power source, destroyed by falling debris.","The wasted energy merely gets absorbed and, with no storage, is rejected as waste heat, so this room is actually quite warm. Strange crystal structures line the gallery to the east, but serve an unknown purpose.");
}

break;


beginstate 15;

if (get_sdf(1,22) == 0) {
 set_flag(1,22,1);
 message_dialog("This is an old storage room. Some supplies were placed here, but it was probably rarely used. Now it is full of cobwebs, bones, and dust. Not even the wandering undead seem to spend much time in here.","");
}

break;


beginstate 16;

if (get_sdf(1,23) == 0) {
 set_flag(1,23,1);
 message_dialog("You find an old alien lab of some sort. Whatever rituals, experiments, or procedures were carried out here, you have no way of knowing. A shattered energy source lies next to a long defunct beam projector.","On the other side of the room appear to be several very old stone pedestals with some glowing texts on them. Unfortunately, most of them seem buried under the rubble; however, one has survived it seems.");
}

break;


beginstate 17;

if (get_sdf(1,24) == 0) {
 set_flag(1,24,1);
 message_dialog("At the center of this room is a very inhuman piece of magical machinery. A wide white pedestal rises from the floor. Atop it rests several golden tentacles with their ends inserted into a brightly glowing orb.","Beside it are two power supplies feeding the machine with immense energy from some unknown source. You feel it radiating some sort of magical field, but you have no idea what it does.");
}

break;


beginstate 18;

if ((get_sdf(5,7) == 0) && (get_sdf(5,6) > 0) && (get_sdf(100,0) == 0)) {
 set_flag(5,7,1);
 message_dialog("This device is holding the Perfect Spirit (and the others) here. You can sense it in the thoughts that are tugging at you. The draining continues without relent. You must destroy this machine!","You take your best swing and try your strongest spells, but to no avail. You notice the energy beams nearby produce a lot energy. You must find a means to redirect the beams in the mine to destroy the relic and fast!");
 end();
}

if ((get_sdf(1,25) == 0) && (get_sdf(5,6) == 0)) {
 set_flag(1,25,1);
 message_dialog("As you approach the mighty orb, the field surrounding it grows very strong. You try to touch the device, but the field deflects any attempt to do so. A strong defense system protects this machine.","You are not sure what it does and it is probably best to stay away from this contraption, at least until you know more about it.");
}

break;


beginstate 19;

if (get_sdf(1,26) == 0) {
 set_flag(1,26,1);
 message_dialog("You enter a very short hallway. The gallery beyond has caved in long ago, destroying whatever alien machinery it may have contained.","");
}

break;


beginstate 20;

reset_dialog();
add_dialog_str(0,"You approach the stone tablet. The strange runes upon it reconfigure into a readable language. You read it.",0);
add_dialog_str(1,"This place was built to trap spirit spies during some long forgotten ancient war. Various traps were placed at strategic locations. When any spirit would near, it would be transported here.",0);
add_dialog_str(2,"This is accomplished by a special device emitting a very strong attractive field that binds the spirits here. Its pull is so strong that no spirit can escape and is effectively trapped here forever.",0);
add_dialog_str(3,"By now you estimate there to be at least several thousand spirits haunting this island, including that maniacal one you encountered. Unfortunately, there is no mention who put these machines here or how to deactivate them.",0);
add_dialog_str(4,"Whoever it was left a fine mess that they did not care to clean up.",0);
add_dialog_choice(0,"OK.");
choice = run_dialog(1);

break;


beginstate 21;

if ((get_sdf(5,6) == 1) && (get_sdf(100,0) == 0)) {
 message_dialog("_No! You cannot leave here. I must escape! I will not be denied!_","A very powerful force tugs and you and keeps you from leaving. You must somehow defeat this creature. There is no escape.");
 block_entry(1);
}

break;


beginstate 22;

if (get_sdf(5,7) == 1) {
 set_flag(5,7,2);
 message_dialog("You approch one of the mirrors and you feel the heat from the intense energy being directed. Suddenly, you have a jolt of insight. You hack at the bottom of the ancient mirror and you are able to release it!","These things are mobile! Now, you need to redirect the energy sources to the spirit trap device. You feel weaker and weaker. Time is running out!");
 set_terrain(14,31,0);
 put_object_on_space(14,31,6);
 set_terrain(13,56,0);
 put_object_on_space(13,56,5);
}

break;